package com.iweb.controller;

import com.common.context.RequestMapping;
import com.iweb.pojo.Community;
import com.iweb.pojo.User;
import com.iweb.service.AdminService;
import com.iweb.service.CommunityService;
import com.iweb.service.UserService;
import com.iweb.utils.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 视图渲染 返回值 是 string
 * void 是 ajax
 */
@RequestMapping("user")
public class UserController {
	@RequestMapping("reg")
	public synchronized String Register(User user, HttpServletRequest request) {
		UserService userService = new UserService();
		boolean flag = userService.addUser(user);
		//如果flag为true，则用户不重复，
		if (flag) {
			return "/user/login.jsp";
		} else {
			request.setAttribute("message", "用户已存在");
			return "/user/reg.jsp";
		}
	}


	@RequestMapping("login")
	public String login(User user, HttpServletRequest request) {
		HttpSession session = request.getSession();

		User sessionUser = null;
		sessionUser = (User) session.getAttribute("user");

		//如果从session中得不到User信息，则要求用户登录
		if (sessionUser == null) {
			UserService userService = new UserService();
			User result = userService.findByUserId(user.getUserid());
			// 业务用户
			if (result == null) {
				request.setAttribute("message", "用户不存在");
				return "/user/login.jsp";
			}
			// 判断密码
			if (!user.getPassword().equals(result.getPassword())) {
				request.setAttribute("message", "密码错误");
				return "/user/login.jsp";
			}
			// 保存用户到session
//		HttpSession session = request.getSession();
			session.setAttribute("user", result);
		}

		return "/user/homepage.jsp";
	}

	@RequestMapping("logout")
	public String logout(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		return "/user/logout.jsp";
	}

	//用户商品结算
	@RequestMapping("checkout")
	public String checkout(User user, HttpServletRequest request) {
		String userid = user.getUserid();
		Double price = user.getBalance();

		UserService userService = new UserService();
		Double balance = userService.checkout(userid, price);
		if (balance == null) {
			request.setAttribute("isPaySuccessful", 0);
			request.setAttribute("balance", 0);
		} else {
			request.setAttribute("isPaySuccessful", 1);
			request.setAttribute("balance", balance.doubleValue());
		}

		return "/user/checkout.jsp";
	}

    @RequestMapping("community")
    public String communityChat(HttpServletRequest request){
        CommunityService communityService = new CommunityService();
        List<Community> list=communityService.findAll();
        request.setAttribute("list",list);
        request.setAttribute("type",0);
        return "/user/community.jsp";
    }

    @RequestMapping("selectZans")
    public String zanCounts(Community community,HttpServletRequest request){
        CommunityService communityService=new CommunityService();
        int type=communityService.updateByName(community.getUsername(),community.getType());
        List<Community> list=communityService.findAll();
        request.setAttribute("list",list);
        request.setAttribute("type",type);
        request.setAttribute("username",community.getUsername());
        return "/user/community.jsp";
//        return null;
    }
}
